using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace quickTask
{
    public partial class CalendarDialog : Form
    {
        public CalendarDialog(ref List<string> theCalList)
        {
            mCalList = theCalList;
            InitializeComponent();
        }

        private void CalendarDialog_Load(object sender, EventArgs e)
        {
            initCalendarListBox();            
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                mCalList.Add(openFileDialog.FileName);
                calendarListBox.Items.Add(Path.GetFileName(openFileDialog.FileName));

                mIsDirty = true;
            }
            
        }


        private void removeButton_Click(object sender, EventArgs e)
        {
            if(calendarListBox.SelectedItem != null)
            {
                if(MessageBox.Show(this, "Delete " + calendarListBox.SelectedItem + "?", 
                    "Delete Calendar", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    == DialogResult.Yes)
                {
                    mCalList.RemoveAt(calendarListBox.SelectedIndex);
                    calendarListBox.Items.RemoveAt(calendarListBox.SelectedIndex);

                    mIsDirty = true;
                }
            }
        }


        private void saveButton_Click(object sender, EventArgs e)
        {
            (Owner as Form1).saveCalList();
            mIsDirty = false;
        }


        private void closeButton_Click(object sender, EventArgs e)
        {
            if(mIsDirty)
            {
                if (MessageBox.Show(this, "Save changes?", "Unsaved Changes", 
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    saveButton_Click(this, EventArgs.Empty);
                }
            }
            Close();
        }

        private void initCalendarListBox()
        {
            foreach(string str in mCalList)
            {                
                calendarListBox.Items.Add(Path.GetFileName(str));
            }
        }

        private List<string> mCalList;
        private bool mIsDirty = false;
    }
}